//
//  SQLowSamePlaceBidViewController.m
//  pai
//
//  Created by pro on 14-11-20.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQLowSamePlaceBidViewController.h"
#import "SQHitTableViewCell.h"

@interface SQLowSamePlaceBidViewController ()

@end

@implementation SQLowSamePlaceBidViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:@"撞价列表"];
    
    //标头
    UIImageView* titleimg = [[UIImageView alloc]initWithFrame:CGRectMake(10,self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height + 20, 299, 33)];
    [titleimg setImage:[UIImage imageNamed:@"hit_title"]];
    [self.view addSubview:titleimg];
    
    //列表
    self.hitTableView = [[UITableView alloc] init];;
    self.hitTableView.frame =CGRectMake(0,titleimg.frame.origin.y+titleimg.frame.size.height, 300, [[UIScreen  mainScreen] bounds].size.height - 44 - titleimg.frame.origin.y-titleimg.frame.size.height - 20);
    self.hitTableView.tag = 2000;
    self.hitTableView.delegate = self;
    self.hitTableView.dataSource = self;
    [self.hitTableView setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1]];
    self.hitTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.hitTableView];
    
    [self getInfo];
}

-(void)getInfo
{
    my_stuff_t stuff;
    stuff.base.msgtype = 1030;
    stuff.base.len = sizeof(stuff);
    stuff.base.error = 0;
    stuff.type = 3001;
    
    stuff.auctprice = (int)self.auctprice;
    
    strcpy(stuff.serialnum, [self.serialnum UTF8String]);
    strcpy(stuff.username, [[Global instance].userinfo.username UTF8String]);
    
    NSLog(@"%d",stuff.auctprice);
    NSLog(@"%s",stuff.username);
    NSLog(@"%s",stuff.serialnum);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&stuff length:sizeof(stuff)];
    
    [super sendData:msgData tag:1 withHUD:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma  mark UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataMarr.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 33;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *orderCellName  = @"SQHitTableViewCell";
    SQHitTableViewCell *ordercell = (SQHitTableViewCell *)[tableView dequeueReusableCellWithIdentifier:orderCellName];
    if (ordercell==nil) {
        ordercell = [[[NSBundle mainBundle] loadNibNamed:@"SQHitTableViewCell" owner:self options:nil] lastObject];
    }
    hit_t item;
    [[self.dataMarr objectAtIndex:indexPath.row]getValue:&item];
    [ordercell setItem:item];
    ordercell.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    return ordercell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSLog(@"get %ld",tag);
    if (tag == 1) {
        if (self.socketLength == 0) {
            ack_my_stuff_t baseInfo;
            [data getBytes:&baseInfo length:sizeof(baseInfo)];
            NSLog(@"%d",baseInfo.base.msgtype);
            NSLog(@"%d",baseInfo.base.len);
            NSLog(@"%d",baseInfo.base.error);
            NSLog(@"%lu",(unsigned long)data.length);
            self.templength =  data.length;
            self.socketLength = baseInfo.base.len;
        }
        else{
            self.templength += data.length;
        }
        if (self.socketTempData == nil) {
            self.socketTempData = [NSMutableData dataWithData:data];
        }else{
            [self.socketTempData appendData:data];
        }
        if (self.templength < self.socketLength) {
            [asyncSocket readDataWithTimeout:-1 tag:tag];
            return;
        }else{
            ack_my_stuff_t baseInfo;
            [self.socketTempData getBytes:&baseInfo length:sizeof(baseInfo)];
            hit_t hit;
            self.dataMarr = nil;
            self.dataMarr = [NSMutableArray array];
            for (int i =0; i< baseInfo.count; i++) {
                [self.socketTempData getBytes:&hit range:NSMakeRange(self.socketTempData.length - (baseInfo.count - i)*sizeof(hit_t)-8,sizeof(hit_t))];
                [self.dataMarr addObject:[NSValue value:&hit withObjCType:@encode(struct hit_t)]];
            }
            self.socketTempData = nil;
            self.socketLength = 0;
            self.templength = 0;
            //更新列表
            [self.hitTableView reloadData];
        }
    }
}
@end
